Skip to content

[Metal] Keep intermediate values in a float threadgroup buffer for the hadamard_n kernel - #5006

Merged
zcbenz merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/hadamard-float-intermediates
Nov 29, 2026
Merged

zcbenz merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/hadamard-float-intermediates

Conversation

@aleroot

@aleroot aleroot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Fix the precision of the hadamard_n kernel and contextually improve its performance of single row (decode): 4–20% faster.

I have realised this while working on ml-explore/mlx-swift-lm#835 so I decided to propose this here, so that if it gets accepted and merged in the future I could simplify the implementation SignedBlockHadamardKernel and also the VarianceNormalizedKVCache.rotate` in VarianceNormalizedKVCache.swift would get basically improved automatically.

Measurements

Measured on an idle M5 Max: median time relative to upstream, 2462 rows, 8 interleaved runs. Below 1.00× means faster.

block 64 128 256 512 1024 2048 4096 8192
fp16 0.85× 0.89× 1.00× 1.03× 1.01× 0.97× 0.89× 0.65×
bf16 0.82× 0.88× 0.99× 1.04× 0.99× 0.96× 0.87× 0.64×

Single row (decode): around 4–20% faster.

  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure:

@zcbenz
zcbenz force-pushed the alessio/hadamard-float-intermediates branch from 7b6affc to c534a36 Compare November 28, 2026 23:33

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is great that kernel becomes both simpler and faster!

@zcbenz zcbenz changed the title Keep intermediate values in a float threadgroup buffer for the hadamard_n kernel [Metal] Keep intermediate values in a float threadgroup buffer for the hadamard_n kernel Nov 29, 2026
@zcbenz
zcbenz merged commit 88d6aba into ml-explore:main Nov 29, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants